﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MacomberMapSystem.Common.Serialization;
using System.Data.Common;
using System.Drawing;

namespace MacomberMapSystem.Common.Database
{
    /// <summary>
    /// This class holds information on a one-line assignment
    /// </summary>
    public class MM_Database_Assignment: MM_Serializable
    {
        #region Variable declarations
        /// <summary>The one-line that's needing editing</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public int OneLine;              

        /// <summary>The model needing modification</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public MM_Database_Model Model;

        /// <summary>The user assigned to the one-line</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public MM_Database_User AssignedTo;

        /// <summary>When the one-line was assigned</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public DateTime AssignedOn;

        /// <summary>When the one-line was completed</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public DateTime CompletedOn;

        /// <summary>The user who completed the one-line</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public MM_Database_User CompletedBy;

        /// <summary>When the one-line was exported</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public DateTime ExportedOn;

        /// <summary>When the one-line was checked out for editing in accordance with the assignment</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public DateTime CheckedOutOn;       

        /// <summary>The database associated with this one-line</summary>
        public MM_Database_Connector Db;

        /// <summary>The type of one-line this is</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public MM_Database_OneLine.OneLineTypeEnum OneLineType;

        /// <summary>The name of the one-line</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public String OneLineName;

        /// <summary>The number of errors associated with the one-line assignment</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public int Errors;

        /// <summary>The number of warnings associated with the one-line assignment</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public int Warnings;

        /// <summary>
        /// Retrieve the notes for our assignment
        /// </summary>
        public String Notes
        {
            get
            {
                using (DbCommand dCmd = Db.CreateCommand("SELECT NOTES FROM MM_DATABASE_ASSIGNMENT WHERE ID=:0", ID))
                    return dCmd.ExecuteScalar().ToString();
            }
        }

        #endregion

        #region Initialization
        /// <summary>
        /// Load in an assignment
        /// </summary>
        public MM_Database_Assignment(DbDataReader dRd, MM_Database_Connector Db, MM_Repository Repository): base(dRd,null,Repository)
        {
            this.Db = Db;
        }
        #endregion

        #region One-line retrieval
        /// <summary>
        /// Retrieve the one-line image from the database
        /// </summary>
        public Bitmap OneLineImage
        {
            get
            {
                return Db.LoadOneLineImage(OneLine);
            }
        }

        /// <summary>
        /// Retrieve the full one-line from the database
        /// </summary>
        public MM_Database_OneLine GetOneLine(MM_Repository Repository)
        {            
                return Db.LoadOneLine(OneLine,Repository);            
        }
        #endregion      
    }
}
